package com.password.desktop.page;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

import org.apache.commons.lang3.StringUtils;

import com.password.desktop.UI.PMButton;
import com.password.desktop.service.LoginService;
import com.password.desktop.util.MessageUtil;
import com.password.desktop.util.SysUtil;

// https://github.com/atarw/material-ui-swing
// https://www.cnblogs.com/Xiangjiakang/p/10961165.html
/**
 * 登录窗口
 * 
 * @author yzh
 * @date 2020年6月4日
 *
 */
public class LoginWindow extends JFrame implements MouseListener {

	private static final long serialVersionUID = 1L;
	private static final String TIP_USER_NAME = "用户名";
	private static final String TIP_PASSWORD = "密码";

	LoginService loginService = LoginService.builderLoginService();

	JLabel bacgrangd, minW, closeW, QQ, qq, tu;// gif,最小化，关闭，logo，QQ，头像
	JLabel lie1, lie2;// 暗色块|线
	JTextField user;// 账号
	JPasswordField pass;// 密码
	JPanel bgcolor;// 白
	JLabel su1, mi1;// 缩略图
	private JCheckBox rememberMe;// 记住密码
	private JLabel tip; // 提示语句
	static Point origin = new Point();// 变量，用于可拖动窗体
	int a = 0, b = 0, c = 0, d = 0;// 控制线
	int f = 0, g = 0, h = 0, j = 0;// 控制√
	private JLabel register, gest; // 登录框，注册用户，游客登录

	public LoginWindow() {
		// 实例化
		bacgrangd = new JLabel(new ImageIcon(getResoure("素材/1.gif")));
		minW = new JLabel(SysUtil.ICON_MIN);
		closeW = new JLabel(SysUtil.ICON_CLOSE);
		QQ = new JLabel(new ImageIcon(getResoure("images/login/logo.png")));
		qq = new JLabel("PM");
		tu = new JLabel(new ImageIcon(getResoure("images/head/head.png"))); // 头像
		user = new JTextField();
		pass = new JPasswordField();
		su1 = new JLabel(new ImageIcon(getResoure("images/login/middle_logo_gray.png")));
		mi1 = new JLabel(new ImageIcon(getResoure("素材//密码.png")));
		lie1 = new JLabel(new ImageIcon(getResoure("素材//直线2.png")));
		lie2 = new JLabel(new ImageIcon(getResoure("素材//直线2.png")));
		bgcolor = new JPanel();
		rememberMe = new JCheckBox("记住密码");
		tip = new JLabel("用户名或密码错误");
		rememberMe.setBackground(Color.WHITE);
		JLabel submit = new PMButton("登录", PMButton.STYLE_INFO, (e) -> {
			String userName = user.getText();
			String password = new String(pass.getPassword());
			if (StringUtils.isNoneBlank(userName, password) && !userName.equals(TIP_USER_NAME)
					&& !password.equals(TIP_PASSWORD)) {
				try {
					loginService.login(userName, password);
					dispose();
					new MenuWindow();
				} catch (Exception ex) {
					tip.setText(ex.getMessage());
					tip.setVisible(true);
				}
			} else {
				MessageUtil.showMessage(LoginWindow.this, "用户名和密码不能为空");
			}

		});
		// 位置
		bacgrangd.setBounds(-35, -123, 500, 250);
		minW.setBounds(364, 2, 32, 32);
		closeW.setBounds(396, 3, 32, 32);
		QQ.setBounds(10, 10, 32, 32);
		qq.setBounds(50, 5, 45, 45);
		tu.setBounds(170, 80, 90, 85);
		user.setBounds(130, 160, 180, 40);
		pass.setBounds(130, 200, 180, 40);
		su1.setBounds(100, 170, 20, 20);
		mi1.setBounds(100, 210, 20, 20);
		lie1.setBounds(100, 190, 240, 10);
		lie2.setBounds(100, 230, 240, 10);
		bgcolor.setBounds(0, 125, 500, 300);
		rememberMe.setBounds(100, 250, 80, 20);
		tip.setBounds(240, 250, 120, 20);
		submit.setBounds(100, 280, 242, 35);
		// 属性
		qq.setFont(new Font("微软雅黑", Font.BOLD, 20));
		qq.setForeground(Color.white);
		bgcolor.setBackground(new Color(255, 255, 255));

		minW.setBackground(new Color(0, 0, 0, 0.3f));
		minW.setOpaque(false);
		closeW.setBackground(new Color(0, 0, 0, 0.3f));
		closeW.setOpaque(false);

		user.setForeground(Color.gray);
		user.setText(TIP_USER_NAME);
		user.setOpaque(false);// 透明背景
		user.setBorder(null);// 去掉边框
		user.setFont(SysUtil.FONT_DEFAULT);// 框内文字样式
		pass.setFont(SysUtil.FONT_DEFAULT);// 框内文字样式
		pass.setBorder(null);// 去掉边框

		pass.setOpaque(false);// 透明背景
		pass.setForeground(Color.gray);
		pass.setText(TIP_PASSWORD);
		pass.setEchoChar((char) 0);// 让密码显示出来

		tip.setFont(new Font("微软雅黑", 0, 12));
		tip.setVerticalAlignment(JLabel.CENTER);
		tip.setForeground(Color.red);
		tip.setVisible(false);

		rememberMe.setFont(new Font("微软雅黑", 0, 12));
		rememberMe.setForeground(new Color(170, 170, 170));

		// 事件区域
		minW.addMouseListener(this);
		closeW.addMouseListener(this);
		user.addMouseListener(this);
		pass.addMouseListener(this);
		rememberMe.addMouseListener(this);
		this.addMouseListener(this);

		this.addMouseMotionListener(new MouseMotionListener() {// 窗体拖动事件
			public void mouseMoved(MouseEvent e) {
			}

			public void mouseDragged(MouseEvent e) {
				Point p = getLocation();
				setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
			}

		});

		user.addFocusListener(new FocusListener() {

			public void focusLost(FocusEvent e) {// 失去焦点
				su1.setIcon(new ImageIcon(getResoure("images/login/middle_logo_gray.png")));
				lie1.setIcon(new ImageIcon(getResoure("素材/直线2.png")));
				c = 0;
				if (user.getText().isEmpty()) {// 判断是否为空（为了设置默认提示语）
					user.setForeground(Color.gray);
					user.setText(TIP_USER_NAME);
				}
			}

			public void focusGained(FocusEvent e) {// 得到焦点
				user.setForeground(Color.black);
				lie1.setIcon(new ImageIcon(getResoure("素材/直线3.png")));
				a = 1;
				c = 1;
				b = 0;
				su1.setIcon(new ImageIcon(getResoure("images/login/middle_logo.png")));
				if (user.getText().equals(TIP_USER_NAME)) {
					user.setText("");
				} else {
					user.setText(user.getText());
					user.selectAll();
				}
			}
		});

		pass.addFocusListener(new FocusListener() {

			public void focusLost(FocusEvent e) {// 失去焦点
				lie2.setIcon(new javax.swing.ImageIcon(getResoure("素材//直线2.png")));// 失去焦点换图片
				mi1.setIcon(new javax.swing.ImageIcon(getResoure("素材//密码.png")));
				d = 0;
				if (pass.getText().isEmpty()) {
					pass.setForeground(Color.gray);
					pass.setText(TIP_PASSWORD);
					pass.setEchoChar((char) 0);// 让密码显示出来
				}
			}

			public void focusGained(FocusEvent e) {// 得到焦点
				mi1.setIcon(new javax.swing.ImageIcon(getResoure("素材//密码 (1).png")));
				lie2.setIcon(new javax.swing.ImageIcon(getResoure("素材//直线3.png")));
				b = 1;
				a = 0;
				d = 1;
				pass.setForeground(Color.black);
				pass.setEchoChar('*');// 让用户输入看不见
				if (pass.getText().equals(TIP_PASSWORD)) {
					pass.setText("");
				} else {
					pass.setText(pass.getText());
				}
			}
		});

		getContentPane().setLayout(null);// 布局

		getContentPane().add(minW);
		getContentPane().add(closeW);
		getContentPane().add(qq);
		getContentPane().add(QQ);
		getContentPane().add(tu);
		getContentPane().add(lie1);
		getContentPane().add(lie2);
		getContentPane().add(user);
		getContentPane().add(pass);
		getContentPane().add(su1);
		getContentPane().add(mi1);
		getContentPane().add(tip);
		getContentPane().add(rememberMe);
		getContentPane().add(submit);

		register = new PMButton("注册账号", PMButton.STYLE_BULE_BORDER, (e) -> {
			dispose();
			new RegisterWindow();
		});
		register.setBounds(100, 330, 110, 35);
		getContentPane().add(register);

		gest = new PMButton("游客登录", PMButton.STYLE_BULE_BORDER, (e) -> {
			try {
				loginService.loginOnGuest();
				dispose();
				new MenuWindow();
			} catch (Exception ex) {
				MessageUtil.showMessage(null, ex.getMessage());
			}
			MessageUtil.showMessage(null, "使用游客登录");
		});
		gest.setBounds(232, 330, 110, 35);
		getContentPane().add(gest);
		getContentPane().add(bgcolor);

		getContentPane().add(bacgrangd);
		this.setSize(430, 380);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage(getResoure("images/login/logo.png")));// 窗体图标
		this.setLocationRelativeTo(null);// 保持居中
		this.setUndecorated(true);// 去顶部
		this.setFocusable(true);// 面板首先获得焦点
		this.setBackground(new Color(255, 255, 255));// 背景颜色
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setAlwaysOnTop(true);// 最顶层
		this.setVisible(true);// 显示
	}

	public static void main(String[] args) {

		new LoginWindow();

	}

	public void mouseClicked(MouseEvent e) {// 点击不恢复
	}

	public void mousePressed(MouseEvent e) {// 点击后
		if (e.getSource() == minW) {
			setExtendedState(JFrame.ICONIFIED);
		} else if (e.getSource() == this) {
			origin.x = e.getX();
			origin.y = e.getY();
		} else if (e.getSource() == closeW) {
			System.exit(0);
		} else if (e.getSource() == rememberMe) {
			if (h == 0) {
				j = 1;
				h = 1;
			} else if (j == 1) {
				h = 0;
				j = 0;
			}
		}
	}

	public void mouseReleased(MouseEvent e) {// 点击时
	}

	public void mouseEntered(MouseEvent e) {// 悬停
		if (e.getSource() == minW) {
			minW.setOpaque(true);
		} else if (e.getSource() == closeW) {
			closeW.setOpaque(true);
		} else if (e.getSource() == user) {
			if (a == 0 && c == 0) {
				lie1.setIcon(new ImageIcon(getResoure("素材//直线4.png")));
			}
		} else if (e.getSource() == pass) {
			if (b == 0 && d == 0) {
				lie2.setIcon(new ImageIcon(getResoure("素材//直线4.png")));
			}
		}
	}

	public void mouseExited(MouseEvent e) {// 悬停后
		if (e.getSource() == minW) {
//			an1.setOpaque(false);
			minW.setOpaque(false);
		} else if (e.getSource() == closeW) {
//			an2.setOpaque(false);
			closeW.setOpaque(false);
		} else if (e.getSource() == user) {
			if (a == 0) {
				lie1.setIcon(new ImageIcon(getResoure("素材//直线2.png")));
			}
		} else if (e.getSource() == pass) {
			if (b == 0) {
				lie2.setIcon(new ImageIcon(getResoure("素材//直线2.png")));
			}
		}

	}

	public URL getResoure(String path) {
		return this.getClass().getClassLoader().getResource(path);
	}
}